package cn.iocoder.yudao.module.trade.controller.admin.fruitcard;

import cn.iocoder.yudao.module.trade.controller.admin.fruitcard.vo.*;
import cn.iocoder.yudao.module.trade.convert.fruitcard.FruitCardUserConvert;
import cn.iocoder.yudao.module.trade.dal.mysql.fruitcard.FruitCardUserDO;
import cn.iocoder.yudao.module.trade.service.fruitcard.FruitCardUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 水果卡用户")
@RestController
@RequestMapping("/fruit/card-user")
@Validated
public class FruitCardUserController {

    @Resource
    private FruitCardUserService cardUserService;

    @PostMapping("/create")
    @Operation(summary = "创建水果卡用户")
    @PreAuthorize("@ss.hasPermission('fruit:card-user:create')")
    public CommonResult<Long> createCardUser(@Valid @RequestBody FruitCardUserCreateReqVO createReqVO) {
        return success(cardUserService.createCardUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水果卡用户")
    @PreAuthorize("@ss.hasPermission('fruit:card-user:update')")
    public CommonResult<Boolean> updateCardUser(@Valid @RequestBody FruitCardUserUpdateReqVO updateReqVO) {
        cardUserService.updateCardUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水果卡用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fruit:card-user:delete')")
    public CommonResult<Boolean> deleteCardUser(@RequestParam("id") Long id) {
        cardUserService.deleteCardUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水果卡用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fruit:card-user:query')")
    public CommonResult<FruitCardUserRespVO> getCardUser(@RequestParam("id") Long id) {
        FruitCardUserDO cardUser = cardUserService.getCardUser(id);
        return success(FruitCardUserConvert.INSTANCE.convert(cardUser));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水果卡用户列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fruit:card-user:query')")
    public CommonResult<List<FruitCardUserRespVO>> getCardUserList(@RequestParam("ids") Collection<Long> ids) {
        List<FruitCardUserDO> list = cardUserService.getCardUserList(ids);
        return success(FruitCardUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水果卡用户分页")
    @PreAuthorize("@ss.hasPermission('fruit:card:query')")
    public CommonResult<PageResult<FruitCardUserRespVO>> getCardUserPage(@Valid FruitCardUserPageReqVO pageVO) {
        return success(cardUserService.getCardUserPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水果卡用户 Excel")
    @PreAuthorize("@ss.hasPermission('fruit:card:export')")
    @OperateLog(type = EXPORT)
    public void exportCardUserExcel(@Valid FruitCardUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FruitCardUserDO> list = cardUserService.getCardUserList(exportReqVO);
        // 导出 Excel
        List<FruitCardUserExcelVO> datas = FruitCardUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水果卡用户.xls", "数据", FruitCardUserExcelVO.class, datas);
    }


    @PutMapping("/update-status")
    @Operation(summary = "更新水果卡状态")
    @PreAuthorize("@ss.hasPermission('fruit:card:update')")
    public CommonResult<Boolean> updateCardStatus(@Valid @RequestBody FruitCardUpdateStatusReqVO updateReqVO) {
        cardUserService.updateCardStatus(updateReqVO);
        return success(true);
    }
}
